package com.msb.c11;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: xw
 * @create: 2022/10/3 14:55
 * @description:ReentrantLock还可以指定为公平锁
 */
public class Code05_ReentrantLock5 extends Thread{
    private static ReentrantLock lock = new ReentrantLock(true);//参数为true表示为公平锁，请对比输出结果

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName()+"获得锁");
            }finally{
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        Code05_ReentrantLock5 t = new Code05_ReentrantLock5();
        Thread th1=new Thread(t);
        Thread th2=new Thread(t);
        th1.start();
        th2.start();
    }
}
